package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocCategoryDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.VocCategoryCommand;
import com.cskaoyan.wordmemorize.service.VocCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/voc")
public class AdminVocCategoryController {


    @Autowired
    VocCategoryService vocCategoryService;


    @GetMapping("category/list")
    public Result<PageDTO<VocCategoryDTO>> getCategoryList(PageRequest pageRequest) {
        PageDTO<VocCategoryDTO> bookCategories = vocCategoryService.getBookCategories(pageRequest);
        return Result.ok(bookCategories);
    }

    @GetMapping("category")
    public Result<VocCategoryDTO> getCategory(Long categoryId) {
        VocCategoryDTO bookCategory = vocCategoryService.getBookCategory(categoryId);
        return Result.ok(bookCategory);
    }

    @SaAdminCheckPermission("admin:category:add")
    @PostMapping("category")
    public Result addCategory(@RequestBody VocCategoryCommand vocCategoryCommand) {
        vocCategoryService.addBookCategory(vocCategoryCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:category:update")
    @PutMapping("category")
    public Result updateBookCategory(@RequestBody VocCategoryCommand vocCategoryCommand) {
        vocCategoryService.updateBookCategory(vocCategoryCommand);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:category:delete")
    @DeleteMapping("category/{categoryId}")
    public Result deleteBookCategory(@PathVariable("categoryId") Long categoryId) {
        vocCategoryService.deleteBookCategory(categoryId);
        return Result.ok();
    }

    @SaAdminCheckPermission("admin:category:batchdelete")
    @DeleteMapping("categories")
    public Result deleteBookCategories(@RequestBody BatchDeleteCommand ids) {
        vocCategoryService.deleteBookCategories(ids);
        return Result.ok();
    }
}
